import {computed, ref} from "vue";
import Teacher from "../api/modules/Teacher";

/**
 * 获取所有教师列表
 * @param auto
 * @param limit
 * @returns {{onGetTypes: ((function(): Promise<void>)|*), typesData: Ref<UnwrapRef<*[]>>}}
 */
export function useTeachers(auto = true, limit = 99999) {
    const teacherData = ref([]);

    if (auto) {
        onGetTeachers();
    }

    const teacherMap = computed(() => {
        const res = {}
        for (let item of teacherData.value) {
            res[item.id] = item.name;
        }
        return res;
    })

    // 获取所有课程分类
    async function onGetTeachers() {
        try {
            const {data} = await Teacher.lists({
                filter: "",
                index: 1,
                limit,
            });
            teacherData.value = data.data || [];
        } catch (e) {
            console.log(e);
        }
    }

    return {
        teacherMap,
        teacherData,
        onGetTeachers,
    }
}
